    import "../style/company.css"
    import conf from '../config/userconf.js'
  export default {
    data() {
      return {
          list:[],
          searchData:{
                anm_branch_name:'',
                start_time:'',
                end_time:'',
          },
          form:{
              anm_branch_name:'',
              anm_branch_address:'',
              anm_legal:'',
              anm_legal_phone:'',
              anm_principal:'',
              anm_principal_phone:'',
              anm_business_license:'111',
              anm_identity_face:'111',
              anm_identity_side:'111',
              anm_grade:'',
              anm_years:'',
              anm_status:''
          },
          paydata:[],
          payForm:{
             anm_id:'',
             anm_grade:'',
             anm_years:'',
          },
          rules:{
            anm_branch_name: [
                { required: true, message: '请输入公司名字', trigger: 'blur' }
            ],
            anm_branch_address: [
                { required: true, message: '请输入公司地址', trigger: 'blur' }
            ],
            anm_legal: [
                { required: true, message: '请输入公司法人名字', trigger: 'blur' }
            ],
            anm_legal_phone: [
                { required: true, message: '请输入法人电话', trigger: 'blur' }
            ],
            anm_principal: [
                { required: true, message: '请输入负责人名字', trigger: 'blur' }
            ],
            anm_principal_phone: [
                { required: true, message: '请输入负责人电话', trigger: 'blur' }
            ],
            anm_grade: [
                { required: true, message: '请选择等级', trigger: 'change' }
            ],
            anm_years: [
                { required: true, message: '请输入购买的年限', trigger: 'blur' }
            ],
            anm_status: [
                { required: true, message: '请选择账号状态', trigger: 'change' }
            ],
          },
          payRules:{
            anm_years: [
                { required: true, message: '请输入购买年限', trigger: 'blur' }
            ],
            anm_grade: [
                { required: true, message: '请选择购买类型', trigger: 'change' }
            ],
          },
          multipleSelection: [],
          dialogtitle:'新增账号',
          centerDialogVisible:false,
          payDialogVisible:false
      }
    },
    filters:{
        mmstatus:(val)=> {
            switch (parseInt(val)) {
                case 1:
                    return '新增'
                case 2:
                    return '续费'

            }
        }
     }, 
    methods: {
        /* 列表*/
        getList (data=null){
            conf.company.list(data,(state,res)=>{
                if (state == 'success'&&res.data.code == 0){
                    this.list = res.data.data.list.data
                }
            })
        },
        accountState (row, column, cellValue, index) {
            switch (cellValue) {
                case 1:
                    return '启用'
                case 2:
                    return '禁用'
            }
        },
        accountFunc (row, column, cellValue, index) {
            switch (cellValue) {
                case 1:
                    return '全部功能'
                case 2:
                    return '部分功能'
                case 3:
                    return '少部分功能'
            }
        },
        /*查询 */
        search () {
            let data  = this.searchData;
            data.start_time = this.transTime(data.start_time)
            data.end_time = this.transTime(data.end_time)

            this.getList(data)
            this.$router.push({name:'pay'})
        },
        /* 时间转化 */
        transTime (val) {
            let time = val? Date.parse(new Date(val)):''
            return  time
        },
        /*取消 */
        cancel (formName) {
            this.centerDialogVisible = false;
            if (formName) {
                this.$refs[formName].resetFields()
            }
        },
        /*提交 */
        submit (formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.judgeprimise();
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        /*判断提交 */
        judgeprimise () {
            if (this.dialogtitle == '新增账号') {
                conf.company.add(this.form,(state,res)=>{
                    if (state == 'success'&&res.state == 0){
                        this.getList()
                        this.centerDialogVisible = false
                    }
                })
            }else {
                conf.company.edit(this.form,(state,res)=>{
                    if (state == 'success'&&res.state == 0){
                        this.getList()
                    }
                })
            }
        },
        /*增 */
        add (formName) {
            this.dialogtitle = '新增账号'
            this.centerDialogVisible = true;
        },
        /*选择 */
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        /*删 */
        del () {
            let data = this.multipleSelection
            conf.resources.del(data,(state,res)=>{
                if (state == 'success'&&res.data.state == 0){
                    this.getList()
                }
            })
        },
        deltest (index,rows) {
            rows.splice(index, 1)
        },
        /*续费*/
        edit (val) {
            this.form={
              anm_id:val.anm_id,
              anm_branch_name:val.anm_branch_name,
              anm_branch_address:val.anm_branch_address,
              anm_legal:val.anm_legal,
              anm_legal_phone:val.anm_legal_phone,
              anm_principal:val.anm_principal,
              anm_principal_phone:val.anm_principal_phone,
              anm_business_license:val.anm_business_license,
              anm_identity_face:val.anm_identity_face,
              anm_identity_side:val.anm_identity_side,
              anm_grade:val.anm_current_grade?val.anm_current_grade.toString():'',
              anm_years:val.anm_years,
              anm_status:val.anm_status?val.anm_status.toString():''
          },
            this.dialogtitle = '编辑账号';
            this.centerDialogVisible = true
        },
        payrecord (val) {
            this.dialogtitle = '续费记录';
            this.payForm.anm_id = val.anm_id;
            this.getrenewlist({anm_id:val.anm_id},()=>{this.centerDialogVisible = true})
        },
        /*续费列表 */
        getrenewlist (data,calback=null) {
            conf.renew.list(data,(state,res)=>{
                if (state == 'success'&&res.data.code == 0){
                      this.paydata = res.data.data.data;
                      if(calback) {
                          calback();
                      }
                }
            })
        },
        /*打开续费 */
        openpay () {
            this.payDialogVisible = true;
        },
        /*续费 */
        pay (formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.payDialogVisible = false;
                    conf.renew.pay(this.payForm,(state,res)=>{
                        if (state == 'success'&&res.data.state == 0){
                            this.paydata = res.data.data;
                            this.getList()
                            this.getrenewlist({anm_id:this.payForm.anm_id});
                        }
                    })
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        /*时间选择显示控件 */
        Timecontrol (val) {
            if(val) {
                this.Timef = false
            }
        },
        /*上传营业执照成功*/
        handleLicenseSuccess(res, file) {
            this.anm_business_license = URL.createObjectURL(file.raw);
        },
        /*上传营业执照*/
        beforeLicenseUpload(file) {
            const isJPG = file.type === 'image/jpeg';
            const isLt2M = file.size / 1024 / 1024 < 2;

            if (!isJPG) {
            this.$message.error('上传头像图片只能是 JPG 格式!');
            }
            if (!isLt2M) {
            this.$message.error('上传头像图片大小不能超过 2MB!');
            }
            return isJPG && isLt2M;
        },
        /*上传身份证正面成功*/
        handleFaceSuccess(res, file) {
            this.anm_identity_face = URL.createObjectURL(file.raw);
        },
        /*上传身份证正面*/
        beforeFaceUpload(file) {
            const isJPG = file.type === 'image/jpeg';
            const isLt2M = file.size / 1024 / 1024 < 2;

            if (!isJPG) {
            this.$message.error('上传头像图片只能是 JPG 格式!');
            }
            if (!isLt2M) {
            this.$message.error('上传头像图片大小不能超过 2MB!');
            }
            return isJPG && isLt2M;
        },
         /*上传身份证反面成功*/
        handleBackSuccess(res, file) {
            this.anm_identity_side = URL.createObjectURL(file.raw);
        },
        /*上传身份证反面*/
        beforeBackUpload(file) {
            const isJPG = file.type === 'image/jpeg';
            const isLt2M = file.size / 1024 / 1024 < 2;

            if (!isJPG) {
            this.$message.error('上传头像图片只能是 JPG 格式!');
            }
            if (!isLt2M) {
            this.$message.error('上传头像图片大小不能超过 2MB!');
            }
            return isJPG && isLt2M;
        }
    },

    mounted (){
        this.$nextTick(function(){
            this.getList()
        })
    },
        
  }